/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

#ifndef __IWR_ESTIMATE_WORKER_HH__
#define __IWR_ESTIMATE_WORKER_HH__

#include <boost/optional.hpp>
#include <boost/interprocess/detail/atomic.hpp>
#include <cstdlib>
#include "example.hh"
#include "learningrate.hh"
#include "loss.hh"
#include "regressor.hh"
#include "threadqueue.hh"

namespace flassol
{
  class IwrEstimateWorker
    {
      private:
        RegressionQueue& in;
        RegressionQueue& out;
        Regressor&       regressor;
        uint64_t         thread_no;
        uint64_t         num_threads;
        LearningRate&    eta;
        Loss&            loss;
        float            lambda;
        bool             adaptive;
        bool             test_only;

        double           t;

        static float
        square (float x)
          {
            return x * x;
          }

      public:
        IwrEstimateWorker (RegressionQueue& _in,
                           RegressionQueue& _out,
                           Regressor&       _regressor,
                           uint64_t         _thread_no,
                           uint64_t         _num_threads,
                           LearningRate&    _eta,
                           Loss&            _loss,
                           float            _lambda,
                           bool             _adaptive,
                           bool             _test_only)
          : in (_in),
            out (_out),
            regressor (_regressor),
            thread_no (_thread_no),
            num_threads (_num_threads),
            eta (_eta),
            loss (_loss),
            lambda (_lambda),
            adaptive (_adaptive),
            test_only (_test_only),
            t (0)
          {
          }

        void operator() ()
          {
            std::vector<boost::optional<RegressionExample> >* rvec;

            out.add_writer ();

            while (in.pop (rvec))
              {
                using boost::interprocess::detail::atomic_dec32;
                using boost::interprocess::detail::atomic_read32;

                for (unsigned int n = 0; n < rvec->size (); ++n)
                  {
                    if ((*rvec)[n])
                      {
                        EstimateResultVector& partial = 
                          (*(*rvec)[n]->partials)[thread_no];

                        regressor.get_partial ((*rvec)[n]->f.get (),
                                               thread_no,
                                               num_threads,
                                               partial);

                        if ((*rvec)[n]->label && ! test_only)
                          {
                            EstimateResult result;

                            if (num_threads > 1)
                              {
                                atomic_dec32 ((*rvec)[n]->estimationcount);
                              }

                            result += partial[0];

                            if (num_threads > 1)
                              {
                                while (atomic_read32 ((*rvec)[n]->estimationcount) > 0)
                                  {
                                  }

                                for (uint64_t i = 0; i < num_threads; ++i)
                                  {
                                    if (i != thread_no)
                                      {
                                        result += (*(*rvec)[n]->partials)[i][0];
                                      }
                                  }
                              }

                            if (result.xnorm > 0)
                              {
                                t += (*rvec)[n]->importance;

                                float thiseta = eta.eta (t);
                                float gsq = 
                                  square (  loss.dldp (result.p, *(*rvec)[n]->label) 
                                          * (*rvec)[n]->importance);
                                
                                if (gsq > 0)
                                  {
                                    UpdateInfoVector info (1);

                                    info[0].gsq = gsq;
                                    info[0].adaptive = adaptive;

                                    if (adaptive)
                                      {
                                        regressor.get_adaptive_norm_partial 
                                          ((*rvec)[n]->f.get (),
                                           thread_no,
                                           num_threads,
                                           info);

                                        if (num_threads > 1)
                                          {
                                            (*(*rvec)[n]->norm_partials)[thread_no][0] = 
                                              info[0].adaptivexnorm;

                                            atomic_dec32 ((*rvec)[n]->adaptivecount);
                                            while (atomic_read32 
                                                     ((*rvec)[n]->adaptivecount) > 0)
                                              {
                                              }

                                            for (uint64_t i = 0;
                                                 i < num_threads;
                                                 ++i)
                                              {
                                                if (i != thread_no)
                                                  {
                                                    info[0].adaptivexnorm +=
                                                      (*(*rvec)[n]->norm_partials)[i][0];
                                                  }
                                              }
                                          }
                                      }

                                    std::pair<float, float> update =
                                      loss.invariant_update 
                                        (result.p,
                                         result.wdotx,
                                         result.adotb,
                                         *(*rvec)[n]->label,
                                         (*rvec)[n]->importance * thiseta,
                                         result.anormplusbnorm,
                                         result.xnorm,
                                         adaptive,
                                         info[0].adaptivexnorm,
                                         lambda);

                                    if (fabsf (update.first) > 0)
                                      {
                                        info[0].sh = update.first;
                                        info[0].decay = update.second;

                                        regressor.update_partial ((*rvec)[n]->f.get (),
                                                                  info,
                                                                  thread_no,
                                                                  num_threads);
                                      }
                                  }
                              }
                          }
                      }
                  }

                if (! rvec->back ())
                  {
                    if (thread_no == 0)
                      {
                        out.push (rvec);
                      }
                  }
                else if (num_threads == 1 || atomic_dec32 (rvec->back ()->usecount) == 1)
                  {
                    out.push (rvec);
                  }
              }

            out.del_writer ();
          }
    };
}

#endif // __IWR_ESTIMATE_WORKER_HH__
